package com.renit.gallery.adapter

import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.renit.gallery.PhotoItem
import com.renit.gallery.R
import com.renit.gallery.TAG

class PagePhotoListAdapter: ListAdapter<PhotoItem, PagePhotoViewHolder>(DiffCallBack) {

    object DiffCallBack: DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagePhotoViewHolder {
        LayoutInflater
            .from(parent.context)
            .inflate(R.layout.page_photo_view, parent, false)
            .apply {
            return PagePhotoViewHolder(this);
        }
    }

    override fun onBindViewHolder(holder: PagePhotoViewHolder, position: Int) {
        Glide
            .with(holder.itemView)
            .load(getItem(position).fullUrl)
            .placeholder(R.drawable.ic_baseline_photo_size_select_actual_24)
            .into(holder.photoDetails)
    }
}

class PagePhotoViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val photoDetails: ImageView = itemView.findViewById(R.id.photo_details)
}